package com.cw.serviceimpl;

import com.cw.data.PageBean;
import com.cw.mapper.TbSpecificationMapper;
import com.cw.mapper.TbSpecificationOptionMapper;
import com.cw.pojo.SpecVO;
import com.cw.pojo.TbSpecification;
import com.cw.pojo.TbSpecificationOption;
import com.cw.service.SpacificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class SpacificationServiceImpl implements SpacificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public PageBean<TbSpecification> getSpacificationInfo(Integer pageNumber, Integer pageSize, String sname) {

        PageHelper.startPage(pageNumber, pageSize);

        List<TbSpecification> tbSpecificationList = specificationMapper.getSpacificationInfo(sname);

        PageInfo<TbSpecification> pa = new PageInfo<>(tbSpecificationList);

        return new PageBean<TbSpecification>(pa.getPageNum(), pa.getPageSize(), pa.getPages(), pa.getTotal(), pa.getList());
    }

    @Override
    @Transactional
    public void addSpec(List<TbSpecificationOption> optionList, TbSpecification tbSpecification) {

        specificationMapper.addSpecInfo(tbSpecification);

        specificationOptionMapper.addSpecInfo(tbSpecification.getId(), optionList);
    }

    @Override
    public SpecVO getOneSpecInfo(Long id) {

        SpecVO specVO = new SpecVO();

        TbSpecification tbf = specificationMapper.selectByPrimaryKey(id);

        List<TbSpecificationOption> tbo = specificationOptionMapper.getSpecInfo(id);

        specVO.setTbSpecification(tbf);

        specVO.setOptionList(tbo);

        return specVO;
    }

    @Override
    @Transactional
    public void updateSpec(List<TbSpecificationOption> optionList, TbSpecification tbSpecification) {

        specificationMapper.updateByPrimaryKeySelective(tbSpecification);

        specificationOptionMapper.deleteSpecInfo(tbSpecification.getId());

        specificationOptionMapper.addSpecInfo(tbSpecification.getId(), optionList);

    }

    @Override
    public void deleteSpec(Long[] id) {

        specificationMapper.deleteSpec(id);

        specificationOptionMapper.deleteInfo(id);
    }

    @Override
    public List<TbSpecificationOption> getSpecInfo(Long id) {
        return specificationOptionMapper.getSpecInfo(id);
    }

    @Override
    public TbSpecification getSpec(Long id) {
        return specificationMapper.selectByPrimaryKey(id);
    }
}
